package com.winai.launcher.override;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.AudioManager;
import android.util.AttributeSet;

import com.winai.launcher.BroadcastReceiver.SystemEventReceiver;
import com.winai.launcher.R;

public class VolumnView extends android.view.View {
    Paint paint;
    Bitmap bitmap;
    private int mMaxVolume;

    // 背景半径
    float r1 = 0;
    // 圆环半径
    float r2 = 0;
    // 白色圆环的宽度
    float w = 5f;
    // 进度
    float progress = 0;

    public VolumnView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public VolumnView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VolumnView(Context context) {
        super(context);
        init(context);
    }

    void init(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Service.AUDIO_SERVICE);
        mMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.STROKE);
        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_volume_open);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        float cx = getMeasuredWidth() / 2;
        float cy = getMeasuredHeight() / 2;
        r1 = cx - w;
        r2 = cx - w * 2.5f;

        // 绘制背景圆
        paint.setColor(Color.parseColor("#d11d1d1d"));
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(cx, cy, r1, paint);

        // 绘制中间的图片
        canvas.drawBitmap(bitmap, cx - bitmap.getWidth() / 2
                , cx - bitmap.getHeight() / 2, paint);

        // 绘制白色进度条
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(w);
        paint.setColor(Color.WHITE);
        RectF oval = new RectF(cx - r2, cy - r2, cx + r2, cy + r2); // 用于定义的圆弧的形状和大小的界限
        canvas.drawArc(oval, 270, 360 * progress / mMaxVolume, false, paint);

        super.onDraw(canvas);
    }

    /**
     * 设置进度
     *
     * @param progress 范围(0-100)
     */
    public void setProgress(float progress) {
        this.progress = progress;
        if (this.progress >= 100)
            this.progress = 100;
        if (this.progress <= 0)
            this.progress = 0;
        setVolumeDialogIcon();
        postInvalidate();
    }

    private void setVolumeDialogIcon() {
        int resourceId;
        if (!SystemEventReceiver.muted && this.progress != 0) {
            resourceId = R.mipmap.icon_volume_open;
        } else {
            resourceId = R.mipmap.icon_volume_silence;
        }
        bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
    }
}
